from datetime import datetime
from django.contrib.auth.models import User
from django.db import models
from django.template.defaultfilters import slugify
from l2ldjango.apps.category.models import Category
from l2ldjango.settings import MEDIA_ROOT

class BlogComments(models.Model):
    creation_date = models.DateTimeField(verbose_name='creation date', auto_now_add=True)
    commented_by=models.ForeignKey(User,null=True,blank=True)
    comment=models.TextField(null=False,blank=False,verbose_name="Comment Here *")

class BlogComments__(models.Model):
    #blog=models.ForeignKey(Blog)
    creation_date = models.DateTimeField(verbose_name='creation date', auto_now_add=True)
    name=models.CharField(max_length=255,null=False,blank=False,verbose_name="Your Name *")
    email=models.EmailField(null=False,blank=False,verbose_name="Your Email *")
    comment=models.TextField(null=False,blank=False,verbose_name="Comment Here *")
    
 
class Meta(models.Model):
    keywords=models.CharField(verbose_name="Keywords",max_length=250,null=True,blank=True)
    description=models.TextField(verbose_name="Description",null=True,blank=True)
    #additional_meta=models.TextField(verbose_name="Additional_Meta",null=True,blank=True)
           
class Blog(Meta):
    """Base design for publishing Blog"""
    STATUS_CHOICES = ((1,'DRAFT'),
                      (2, 'HIDDEN'),
                      (3,'PUBLISHED'))

    title = models.CharField(verbose_name='title', max_length=250)

    image = models.ImageField( verbose_name='image', upload_to='blogimages',
                              blank=True, help_text='used for illustration')
    content = models.TextField(verbose_name='content',blank=False,null=False)
    excerpt = models.TextField(verbose_name='excerpt', blank=True,help_text='optional element')
    slug = models.SlugField(help_text='used for publication',max_length=220)
    #authors = models.ManyToManyField(User, verbose_name='authors',blank=True, null=False)
    comments=models.ManyToManyField(BlogComments,verbose_name='Comments')
    status = models.IntegerField(choices=STATUS_CHOICES, default='DRAFT')
    comment_enabled = models.BooleanField(verbose_name='comment enabled', default=True)
    category=models.ForeignKey(Category,blank=True,null=True) 
    creation_date = models.DateField(auto_now_add=True)
    last_update = models.DateTimeField(verbose_name='last update', auto_now=True)
    start_publication = models.DateTimeField(verbose_name='start publication',help_text='date start publish',auto_now_add=True)
    is_public=models.BooleanField(default=False)

    def get_content(self):
        """Return the content correctly formatted"""
        if not '</p>' in self.content:
            #return linebreaks(self.content) #linebreak wasn't recognized
            return self.content
        return self.content

    def get_comments(self):
        """Return published comments"""
        
        return self.comments

    def save(self):
        super(Blog, self).save()
        #date = datetime.datetime.today()
        #self.slug = '%i/%i/%i/%i-%s' % (date.year, date.month, date.day, self.id, slugify(self.title)
        self.slug = slugify(self.title)
        super(Blog, self).save()

    def __unicode__(self):
        return '%s: %s' % (self.title, self.get_status_display())
    
    